package com.pedro.streamer.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.google.gson.Gson;
import com.pedro.streamer.R;
import com.pedro.streamer.manager.MediaPlayerClient;
import com.pedro.streamer.model.AudioItem;
import com.tencent.mmkv.MMKV;

import java.util.ArrayList;
import java.util.List;

public class AIRadioListAdapter extends RecyclerView.Adapter<AIRadioListAdapter.TopTabViewHolder> {


    private static final String TAG = "com.bmit.engineermode";
    private Context mContext;
    private List<AudioItem> audioItemList;
    private Gson gson;
    private MediaPlayerClient mediaPlayerClient;

    private OnBgmItemClickListener onBgmItemClickListener;

    public interface OnBgmItemClickListener {
        void bgmItemClick();
    }


    public AIRadioListAdapter(Context mContext) {
        this.mContext = mContext;
        this.audioItemList = new ArrayList<>();
        this.gson = new Gson();
        mediaPlayerClient = MediaPlayerClient.getInstance(mContext);
    }

    public void setOnBgmItemClickListener(OnBgmItemClickListener onBgmItemClickListener) {
        this.onBgmItemClickListener = onBgmItemClickListener;
    }

    public void setAudioItemList(List<AudioItem> audioItemList) {
        this.audioItemList.addAll(audioItemList);
        MMKV.defaultMMKV().encode("bgm", this.gson.toJson(this.audioItemList));

        Log.d(TAG, "setAudioItemList: " + MMKV.defaultMMKV().decodeString("bgm"));
        notifyDataSetChanged();
        mediaPlayerClient.updateBgmPlayList(this.audioItemList);
    }


    @Override
    public TopTabViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.radio_list_layout, parent, false);

        return new TopTabViewHolder(view);
    }

    static class TopTabViewHolder extends RecyclerView.ViewHolder {

        View contentView;
        TextView title_text;
        TextView item_delete_text;

        public TopTabViewHolder(View view) {
            //父类构造函数
            super(view);
            contentView = view.findViewById(R.id.content_view);
            title_text = view.findViewById(R.id.title_text);
            item_delete_text = view.findViewById(R.id.item_delete);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull TopTabViewHolder holder, @SuppressLint("RecyclerView") int position) {

        holder.title_text.setText(this.audioItemList.get(position).getAudioName());
        holder.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "点击了: " + audioItemList.get(position).getAudioName());
                mediaPlayerClient.playBgmAtPosition(position);
                if (onBgmItemClickListener != null) {
                    onBgmItemClickListener.bgmItemClick();
                }
            }
        });
        holder.item_delete_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "onClick: " + gson.toJson(audioItemList));
                audioItemList.remove(position);
                Log.d(TAG, "1111111: " + gson.toJson(audioItemList));
                MMKV.defaultMMKV().encode("bgm", gson.toJson(audioItemList));
                notifyDataSetChanged();
                mediaPlayerClient.updateBgmPlayList(audioItemList);
            }
        });
    }
    public void playOrPause(boolean isOpen) {

        mediaPlayerClient.playBgm(isOpen);
    }

    public void changeVolume(float volume) {
        mediaPlayerClient.updateBgmVolume(volume);
    }

    @Override
    public int getItemCount() {

        return audioItemList.size();
    }
}
